﻿// <copyright file="FontPathEntry.cs" company="Sinclair Community College">
// Copyright 2010 Sinclair Community College
// </copyright>
// <originalAuthor>Patrick S. Seymour (patrick.seymour@gmail.com)</originalAuthor>

namespace SinclairCC.AppV
{
    using System;

    /// <summary>
    /// This class represents an entry in the font section of a CP file.
    /// </summary>
    internal class FontPathEntry
    {
        /// <summary>
        /// The size of the entry block in total number of bytes.
        /// </summary>
        /// <remarks>
        /// If the entry size is 0x00000000, the end of the section has been
        /// reached and parsing must be stopped.
        /// </remarks>
        private int entrySize;

        /// <summary>
        /// The size of the entry block header (i.e., all the data before
        /// key/value names and value data).
        /// </summary>
        private int entryBlockHeaderSize;
        
        /// <summary>
        /// This field has the value 0x00000001.
        /// </summary>
        private int entryFlags;
        
        /// <summary>
        /// The relative byte offset to the font path string inside entry block.
        /// </summary>
        /// <remarks>
        /// The offset is relative to the beginning of the entry block header.
        /// </remarks>
        private int pathOffset;
        
        /// <summary>
        /// The size of the path, in bytes.
        /// </summary>
        /// <remarks>
        /// This value does not include a null terminator as there is no null terminator
        /// stored with the path.
        /// </remarks>
        private int pathSize;
        
        /// <summary>
        /// The path of the font file.
        /// </summary>
        private string fontPath;

        /// <summary>
        /// Initializes a new instance of the FontPathEntry class.
        /// </summary>
        /// <param name="dataStream">
        /// A memory stream containing the raw data for this entry.
        /// </param>
        public FontPathEntry(System.IO.MemoryStream dataStream)
        {
            this.ReadSection(dataStream);
        }

        /// <summary>
        /// Gets the size of the entry block in total number of bytes.
        /// </summary>
        public int EntrySize
        {
            get { return this.entrySize; }
        }

        /// <summary>
        /// Gets the size of the entry block header (i.e., all the data
        /// before key/value names and value data).
        /// </summary>
        public int EntryBlockHeaderSize
        {
            get { return this.entryBlockHeaderSize; }
        }

        /// <summary>
        /// Gets the relative byte offset to the font path string inside entry block.
        /// </summary>
        /// <remarks>
        /// The offset is relative to the beginning of the entry block header.
        /// </remarks>
        public int PathOffset
        {
            get { return this.pathOffset; }
        }

        /// <summary>
        /// Gets the size of the path, in bytes.
        /// </summary>
        /// <remarks>
        /// This value does not include a null terminator as there is no null terminator stored with the path.
        /// </remarks>
        public int PathSize
        {
            get { return this.pathSize; }
        }

        /// <summary>
        /// Gets the path of the font file.
        /// </summary>
        public string FontPath
        {
            get { return this.fontPath; }
        }

        /// <summary>
        /// Reads the actual data for this section from the file.
        /// </summary>
        /// <param name="dataStream">
        /// A memory stream containing the raw data for this entry.
        /// </param>
        private void ReadSection(System.IO.MemoryStream dataStream)
        {
            System.IO.BinaryReader streamReader = new System.IO.BinaryReader(dataStream);

            this.entrySize = streamReader.ReadInt32();
            if (this.entrySize > 0)
            {
                this.entryBlockHeaderSize = streamReader.ReadInt32();
                this.entryFlags = streamReader.ReadInt32();
                this.pathOffset = streamReader.ReadInt32();
                this.pathSize = streamReader.ReadInt32();
                this.fontPath = System.Text.Encoding.Unicode.GetString(streamReader.ReadBytes(this.pathSize));
            }
        }
    }
}
